package com.cty.controller;

import com.cty.pojo.Customer;
import com.cty.pojo.ShopReview;
import com.cty.service.ReviewService;
import com.cty.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    @PostMapping("/review")
    @ResponseBody
    public ServerResult saveReview(Integer shopId, Float reviewScore, String reviewText,
                             Float avgPrice,  String dishNames,HttpSession session){
        System.out.println("shopId ="+shopId);
        System.out.println("reviewScore ="+reviewScore);
        System.out.println("reviewText ="+reviewText);
        System.out.println("avgPrice ="+avgPrice);
        System.out.println("dishNames ="+ dishNames);

        Customer customer = (Customer)session.getAttribute("loginCustomer");
        Integer customerId = customer.getCusId();
        System.out.println("customer id:" + customerId);

        ShopReview review = new ShopReview();
        review.setCusId(customerId);
        review.setShopId(shopId);
        review.setReviewScore(reviewScore);
        review.setReviewText(reviewText);
        review.setReviewAvgPrice(avgPrice);
        review.setReviewTime(new Date());
        review.setReviewDishes(dishNames);
        review.setReviewPraise(0);

        ServerResult result = reviewService.save(review);

        return result;


    }
}
